<?php
class PhotosController extends Zend_Controller_Action
{

	public function ajouterphotoAction()
	{
		$form = new Form_AjouterPhoto();
		
		if ($this->getRequest()->isPost()){
    		if ($form->isValid($this->getRequest()->getPost())){
    			$valeurs=$this->getRequest()->getPost();
    			$location = $form->photo->getValue();
    			$uploadtrue = $form->photo->isUploaded();
    			$adrpho = ($uploadtrue)?$location:"imageIndisponible.jpg";
    			$photos = new Table_Photos();
    			$numMax=$photos->getMaxPhoto($valeurs["Num"])->max;
    			$numMax=$numMax+1;
    			$result = $photos->insertPhotos( $numMax,
    											 $valeurs["Num"],
    											 $adrpho,
    											 $valeurs["titre"],
    											 $valeurs["commentaire"]);
    			
    			$this->_redirect('/');
    		}
    	}
		
		$this->view->form = $form;
	}
	
	public function supprimerphotoAction()
	{
		$form = new Form_SupprimerPhoto();
			
		if ($this->getRequest()->isPost()){
    		if ($form->isValid($this->getRequest()->getPost())){
    			$valeurs=$this->getRequest()->getPost();
    			$num = explode('*', $valeurs["photo"]);
    			$photos = new Table_Photos();
    			$endaddr = $photos->getPhoByNumVip($num[0], $num[1])->PHOTO_ADRESSE;
    			$fichier = 'images/photos/'.$endaddr;
    			$result = $photos->deletePhotos( $num[1], $num[0]);
    			if ($endaddr!="imageIndisponible.jpg"){
	    			if(file_exists($fichier))
	     				unlink( $fichier ) ;
    			}
    			$this->_redirect('/');
    		}
    	}
		
		$this->view->form = $form;
	}
	
	static function listevipsAction()
	{
		$vip = new Table_Vips();
    	$listeVips=$vip->getNomPrenomVips();
    	return $listeVips;  
	}
	
	static function listephotosbyvipAction($numvip)
	{
		
		$photo = new Table_Photos();
	    $listePhotos= $photo->getPhotosByNumVip($numvip);
		
    	return $listePhotos;
	}
}